package com.xmut.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xmut.eduservice.entity.EduChapter;
import com.xmut.eduservice.entity.EduVideo;
import com.xmut.eduservice.entity.chapter.ChapterVo;
import com.xmut.eduservice.entity.chapter.VideoVo;
import com.xmut.eduservice.mapper.EduChapterMapper;
import com.xmut.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmut.eduservice.service.EduVideoService;
import com.xmut.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author huangchaoqin
 * @since 2020-08-04
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService eduVideoService;

    // 课程大纲列表
    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
        // 1. 查询本课程所有的章节
        QueryWrapper<EduChapter> queryWrapperchapter = new QueryWrapper<>();
        queryWrapperchapter.eq("course_id", courseId);
        List<EduChapter> eduChapterList = this.list(queryWrapperchapter);
        // 2. 查询本课程所有的小节
        QueryWrapper<EduVideo> queryWrapperVideo = new QueryWrapper<>();
        queryWrapperVideo.eq("course_id", courseId);
        List<EduVideo> eduVideoList = eduVideoService.list(queryWrapperVideo);
        List<ChapterVo> finalList = new ArrayList<>();

        // 3. 遍历查询的章节进行封装

        for (int i = 0; i < eduChapterList.size(); i++) {
            EduChapter eduChapter = eduChapterList.get(i);
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter, chapterVo);
            finalList.add(chapterVo);
            List<VideoVo> videoVoList = new ArrayList<>();
            for (int j = 0; j < eduVideoList.size(); j++) {
                EduVideo eduVideo = eduVideoList.get(j);
                if (eduVideo.getChapterId().equals(eduChapter.getId())) {
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo, videoVo);
                    videoVoList.add(videoVo);
                }
            }
            chapterVo.setChildren(videoVoList);
        }

        // 4. 遍历小节进行封装
        return finalList;
    }

    @Override
    public boolean deleteChapter(String chapterId) {
        // 有小节不删除
        QueryWrapper<EduVideo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("chapter_id", chapterId);
        int count = eduVideoService.count(queryWrapper);
        if (count > 0) { //有小节
            throw new GuliException(20001, "有小节，不能删除该章节");
        } else {  // 米有小节
            boolean b = this.removeById(chapterId);
            return b;
        }
    }

    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        this.remove(queryWrapper);
    }
}
